<?php
/**
 * 
 * App for managing suppliers
 * 
 * @category Abovo
 * 
 * @package Abovo_App_Buy
 * 
 * @subpackage Abovo_App_Buy_Suppliers
 * 
 * @author Antti Holvikari <anttih@gmail.com>
 * 
 * @license http://opensource.org/licenses/gpl-license.php GPL
 * 
 * @version $Id$
 * 
 */

/**
 * 
 * App for managing suppliers
 * 
 * @category Abovo
 * 
 * @package Abovo_App_Buy
 * 
 * @subpackage Abovo_App_Buy_Suppliers
 * 
 */
class Abovo_App_Buy_Suppliers extends Abovo_App_Buy {
    
    /**
     * 
     * Form for editing/adding a supplier
     * 
     * @var Abovo_Form
     * 
     */
    public $supplier_form;
    
    /**
     * 
     * List of suppliers
     * 
     * @var Solar_Sql_Rowset
     * 
     */
    public $suppliers;
    
    /**
     * 
     * Company info
     * 
     * @var Solar_Sql_Row
     * 
     */
    public $company;
    
    /**
     * 
     * Default action
     * 
     * @var string
     * 
     */
    protected $_action_default = 'browse';
    
    /**
     * 
     * Suppliers model
     * 
     * @var Abovo_Model_Supplier
     * 
     */
    protected $_suppliers;
    
    /**
     * 
     * Browse suppliers
     * 
     * @return void
     * 
     */
    public function actionBrowse()
    {
        // get $_GET['keywords]
        if ($keywords = $this->_request->get('keywords', false)) {
            
            // search supliers
            $this->suppliers = $this->_suppliers->search($keywords);
            
            // make sure we stop here
            return;
            
        } else {
            // fetch all
            $this->suppliers = $this->_suppliers->fetchAll();
        }
        
        // fetch tags for each
        foreach ($this->suppliers as $row) {
            $row->tags = $this->_suppliers->tags->fetchAll($row->id);
        }
        
        // set this as active
        $this->layout_local_active = 'buy/suppliers/browse';
    }
    
    /**
     * 
     * Show info about one supplier
     * 
     * @return void
     * 
     */
    public function actionShow($id = null)
    {
        // fetch it
        $info = $this->_suppliers->fetch((int) $id);
        
        // found it?
        if (count($info) === 0) {
            $this->errors[] = $this->locale('ERR_COMPANY_NOT_FOUND');
            $this->_action = 'error';
            $this->_view = 'error';
            return;
        }
        
        $this->company = $info;
        
        // flash notification for company save
        $this->_msgSuccess($this->_session->getFlash('saved'));
        
        // flash notification for company add
        $this->_msgSuccess($this->_session->getFlash('added'));
        
        // set this as active
        $this->layout_local_active = 'buy/suppliers/browse';
    }
    
    /**
     * 
     * Add new supplier
     * 
     * @return void
     * 
     */
    public function actionAdd()
    {
        // get form
        $form = $this->_suppliers->form();
        
        if ($this->_isProcess('save') && $form->validate()) {
            // get submitted values
            $values = $form->values();
            
            // user handle
            $values['company']['handle'] = Solar::registry('user')->auth->handle;
            
            // save
            $info = $this->_suppliers->save($values['company']);
            
            // add flash
            $this->_session->setFlash('added', $this->locale('TEXT_COMPANY_ADDED'));
            
            // write log
            $this->_log(
                "[{$this->user->handle}] Added new supplier "
                . $values['company']['name'] . ' '
                . $values['company']['short']
            );
            
            // forward to company info page
            return $this->_redirect('buy/suppliers/show/' . $info['id']);
        }
        
        // assign form for view
        $this->supplier_form = $form;
        
        $this->layout_local_active = 'buy/suppliers/add';
    }
    
    /**
     * 
     * Edit supplier
     * 
     * @return void
     * 
     */
    public function actionEdit($id = null)
    {
        // we need it to be integer
        settype($id, 'int');
        
        // fetch company info
        $info = $this->_suppliers->fetch($id)->toArray();
        
        // if company not found, redirect to search page
        if (empty($info)) {
            $this->_redirect('buy/suppliers/browse');
        }
        
        // ------------------------------------
        //
        // delete
        //
        
        if ($this->_isProcess('delete')) {
            $this->_suppliers->delete(array('id = ?' => $id));
            $this->_session->setFlash('deleted', $this->locale('TEXT_COMPANY_DELETED'));
            return $this->_forward('search');
        }
        
        // -------------------------------------
        //
        // edit
        //
        
        $form = $this->_suppliers->form();
        
        // save?
        if ($this->_isProcess('save')) {
            if ($form->validate()) {
                // get submitted values
                $values = $form->values();
                
                // set company id
                $values['company']['id'] = $id;
                
                // set user handle
                $values['company']['handle'] = Solar::registry('user')->auth->handle;
                
                // save
                $info = $this->_suppliers->save($values['company']);
                
                // use only the first feedback message
                $this->_session->setFlash('saved', $form->feedback[0]);
                
                // write log
                $this->_log(
                    "[{$this->user['handle']}] Edited supplier "
                    . $values['company']['name'] . ' '
                    . $values['company']['short']
                );
                
                // forward to actionShow() action
                return $this->_redirect('buy/suppliers/show/' . $id);
            }
        } else {
            // set tags as string
            $info['tags'] = implode(', ', $info['tags']);
            
            // populate form from model object
            $form->populate(array('company' => $info));
        }
        
        // assign for view
        $this->supplier_form = $form;
    }
    
    /**
     * 
     * Set up
     * 
     * @return void
     * 
     */
    protected function _setUp()
    {
        parent::_setUp();
        
        // have supplier model available
        $this->_suppliers = Solar::factory('Abovo_Model_Company_Supplier');
        
        // add items to local menu
        $this->layout_local['buy/suppliers/browse'] = $this->locale('APP_ACTION_BROWSE');
        $this->layout_local['buy/suppliers/add']    = $this->locale('APP_ACTION_ADD');
    }
}